<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function index(){
        $this->display();
    }

    public function login(){

        header("Content-Type:text/html; charset=utf-8");

        $user=M('User');//参数的User必须首字母大写，否则自动验证功能失效！
        $username=$_POST['username'];
        $password=$_POST['password'];

        $tmparr = explode('+',$username);
        $date = date('Y-m-d H:i:s');
        $pw = md5($password.$date);
        if(count($tmparr)>1){
            $username = $tmparr[1];
            $usertemp = $user->where("username ='$username'")->find();
            if($usertemp){
                $this->error('用户名已存在');
                return;
            }
            $userdata = array(
                'username' => $username,
                        'password' => $pw,
                        'createtime' => $date
            );
            $user->add($userdata);
            print_r("添加成功");
        }else{
            //查找输入的用户名是否存在
             $user = $user->where("username ='$username'")->find();
            if($user['password'] == md5($password.$user['createtime'])){
                $cookie = array('cookie' => md5($username.$password));
                $usernew = M('User');
                $usernew->data($cookie)->where("username ='$username'")->save();
//                redirect(U('/Admin/index/order/adminname/'.$username),0, '跳转中...');
                $redirectUrl = '/Admin/index/order/adminname/'.$username;
                header('Location: ' . $redirectUrl);
            }else{
                $this->error('用户名或密码错误');
            }

        }



    }

    public function order(){
        $adminname = I('adminname');
        $phone = I('phone');
        $paystatus = I('paystatus');
        $currentpage = I('currentpage');

        $startdate = I('startdate');
        $enddate = I('enddate');

        $user=M('User');
        $user = $user->where("username ='$adminname'")->find();
        if($user && !is_null($user['cookie']) && $user['cookie'] != ""){
            $this->assign("cookie",$user['cookie']);
        }else{
            $redirectUrl = '/Admin/index';
            header('Location: ' . $redirectUrl);
            return;
        }

        $orderMdl = new \Common\Model\LockorderModel();

        $size = 20;
        $wheresql = "1>0";
        if(!is_null($phone) && $phone!=''){
            $wheresql = $wheresql.' and userphone='."'".$phone."'";
        }

        if(!is_null($paystatus) && $paystatus!=''){
            $wheresql = $wheresql.' and paystatus='.$paystatus;
        }

        if(!is_null($startdate) && $startdate!='' && !is_null($enddate) && $enddate!=''){

//            if( $startdate == $enddate){
//                $wheresql = $wheresql.' and createtime='."'".$startdate."'";
//            }else{
                $wheresql = $wheresql.' and createtime>='."'".$startdate." 00:00:00'".' and createtime<='."'".$enddate." 23:59:59'";
//            }

        } else if (!is_null($startdate) && $startdate!=''){
            $wheresql = $wheresql.' and createtime>='."'".$startdate."'";
        } else if (!is_null($enddate) && $enddate!=''){
            $wheresql = $wheresql.' and createtime<='."'".$enddate." 23:59:59'";
        }

        if(is_null($currentpage) || $currentpage ==''){
            $currentpage = 1;
        }

        $startindex = $size*($currentpage -1);

        $orderlist = $orderMdl->getOerdersByPages($wheresql,'createtime desc',$startindex,$size);

        $totalrow = $orderMdl->getCount($wheresql);

        $totalpage = ($totalrow-1)/$size+1;

        $Lock = M('locktable');
        $locklist = $Lock->select();
        $orderlist = $this->formatOrderdate($orderlist,$locklist);



        $this->assign("phone",$phone);
        $this->assign("paystatus",$paystatus);
        $this->assign("startdate",$startdate);
        $this->assign("enddate",$enddate);
        $this->assign("adminname",$adminname);
        $this->assign("totalpage",$totalpage);
        $this->assign("currentpage",$currentpage);
        $this->assign("orders",json_encode($orderlist));
        $this->display();

    }

    public function logout(){
        $adminname = I('adminname');
        $cookie = array('cookie' => '');
        $usernew = M('User');
        $usernew->data($cookie)->where("username ='$adminname'")->save();

        $redirectUrl = '/Admin/index';
        header('Location: ' . $redirectUrl);

    }

    public function formatOrderdate($orderlist,$locklist){


        for ($i = 0; $i< count($orderlist) ; $i++) {
            $orderInfo = $orderlist[$i];
            $content = "";
            $orders = explode('@', $orderInfo['orderdata']);
//            orderdata:lockid-左右规格-内外规格-购买数量-单价
            $index = 0;
            foreach ($orders as $order) {
                $index++;
                $arr = explode('-', $order);
                if ($arr[0] == '666' || $arr[0] == '777') {//网关没左右内外规格
                    $content = $content . '【' . getLockname($locklist, $arr[0])
                        . '，' . $arr[3] . '个】<br>';
                } else {
                    $content = $content . '【' . getLockname($locklist, $arr[0])
                        . '，' . $arr[1] . '，' . $arr[2] . '，' . $arr[3] . '把】<br>';
                }

            }
            $content = str_replace("left", "左开", $content);
            $content = str_replace("right", "右开", $content);
            $content = str_replace("in", "内开", $content);
            $content = str_replace("out", "外开", $content);
            $orderlist[$i]['orderdata'] = $content;

            if($orderlist[$i]['paystatus'] == '0'){
                $orderlist[$i]['paystatus'] = '未支付';
            }else{
                $orderlist[$i]['paystatus'] = '已支付';
            }
            if($orderlist[$i]['paytype'] == 'alipay'){
                $orderlist[$i]['paytype'] = '支付宝';
            }else{
                $orderlist[$i]['paytype'] = '微信支付';
            }
        }

        return $orderlist;
    }


    public function orderexport(){

        $adminname = I('adminname');
//        $phone = I('phone');
        $paystatus = I('paystatus');
        $startdate = I('startdate');
        $enddate = I('enddate');

        $user=M('User');
        $user = $user->where("username ='$adminname'")->find();
        if($user && !is_null($user['cookie']) && $user['cookie'] != ""){
            $this->assign("cookie",$user['cookie']);
        }else{
            $redirectUrl = '/Admin/index';
            header('Location: ' . $redirectUrl);
            return;
        }




        $orderMdl = new \Common\Model\LockorderModel();

        $wheresql = "1>0";

//        if(!is_null($phone) && $phone!=''){
//            $wheresql = $wheresql.' and userphone='."'".$phone."'";
//        }

        if(!is_null($paystatus) && $paystatus!=''){
            $wheresql = $wheresql.' and paystatus='.$paystatus;
        }

        if(!is_null($startdate) && $startdate!='' && !is_null($enddate) && $enddate!=''){

//            if( $startdate == $enddate){
//                $wheresql = $wheresql.' and createtime='."'".$startdate."'";
//            }else{
                $wheresql = $wheresql.' and createtime>='."'".$startdate." 00:00:00'".' and createtime<='."'".$enddate." 23:59:59'";
//            }

        } else if (!is_null($startdate) && $startdate!=''){
            $wheresql = $wheresql.' and createtime>='."'".$startdate."'";
        } else if (!is_null($enddate) && $enddate!=''){
            $wheresql = $wheresql.' and createtime<='."'".$enddate." 23:59:59'";
        }


        $orderlist = $orderMdl->getOrdersByOrder($wheresql,'createtime desc');
        $newOrders = array();
        $Lock = M('locktable');
        $locklist = $Lock->select();
        $orderlist = $this->formatOrderdate($orderlist,$locklist);

        foreach($orderlist as $value){
            $value['orderdata'] = str_replace('<br>', "", $value['orderdata']);  //excel 中不能用 <br> 换行
            $newOrders[] = $value;
        }
        $filename=date("m.d",strtotime($startdate)).'_'.date("m.d",strtotime($enddate)).'订单列表' ;//   导出文件名称
        $title=array(
            'username'=>array("name"=>'客人姓名','width'=>20),
            'userphone'=>array("name"=>'客人手机','width'=>20),
            'totalprice'=>array("name"=>'订单金额','width'=>20),
            'orderdata'=>array("name"=>'订单详情','width'=>47),
            'paytype'=>array("name"=>'支付渠道','width'=>10),
            'from'=>array("name"=>'订单来源','width'=>10),
            'remark'=>array("name"=>'备注','width'=>20),
            'address'=>array("name"=>'地址','width'=>40),
            'createtime'=>array("name"=>'购买时间','width'=>30)
        );
        excel_export($filename, $title, $newOrders);
    }




}